import 'package:flutter/material.dart';
import 'package:gold/route/xfg_nav.dart';
import 'package:gold/ui/my/order/order_config.dart';
import 'package:gold/utils/res.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:gold/utils/xfg_extension.dart';
import 'package:gold/utils/xfg_size.dart';
import 'package:gold/widgets/common_widgets.dart';
import 'package:gold/widgets/xfg_text.dart';

/// Descriptions: 退款原因弹窗
/// 退货理由:0=拍错/多拍/不喜欢,1=与商家协商一致退款,2=克数不对,3=未按约定时间发货,4=快递/物流一直未送到,5=快递/物流无跟踪记录,6=货物破损已拒签
/// User: tangzhentao
/// Date: 4:58 下午 2021/12/7
///
Future<RefundReason?> showRefundReasonDialog({RefundReason? reason}) async {
  return await showModalBottomSheet<RefundReason>(
      context: XfgNav.currentContext,
      isScrollControlled: true,
      builder: (_) => _RefundReasonDialog(reason)
  );
}

class _RefundReasonDialog extends StatelessWidget {
  /// 当前选中监听
  late final ValueNotifier<RefundReason?> _indexNotifier;

  _RefundReasonDialog(RefundReason? reason) {
    _indexNotifier =  ValueNotifier(reason);
  }

  @override
  Widget build(BuildContext context) {
    final bottom = XfgSize().bottomBarHeight == 0 ? XfgSize().size10:XfgSize().bottomBarHeight;
    return Material(
      type: MaterialType.transparency,
      child: Container(
        height: XfgSize().size452 + bottom,
        padding: EdgeInsets.fromLTRB(XfgSize().size10, 0, XfgSize().size10, bottom),
        decoration: BoxDecoration(color: XfgColor.colorE, borderRadius: BorderRadius.vertical(top: Radius.circular(XfgSize().size6))),
        child: Column(
          children: [
            Container(
              height: XfgSize().size60,
              padding: EdgeInsets.symmetric(horizontal: XfgSize().size10),
              child: Row(
                children: [
                  SizedBox.square(dimension: XfgSize().size16),
                  Expanded(child: Center(child: XfgText('退款原因', fontSize: XfgSize().text16, color: XfgColor.text1D, bold: FontWeight.bold))),
                  GestureDetector(
                    onTap: XfgNav.pop,
                    child: Icon(Icons.close, size: XfgSize().size16, color: XfgColor.text2),
                  )
                ],
              ),
            ),
            Container(
              height: XfgSize().size290,
              decoration: white6Decoration,
              child: ValueListenableBuilder<RefundReason?>(
                valueListenable: _indexNotifier,
                builder: (_, select, __) => ListView.builder(
                  itemCount: refundReasonList.length,
                  itemBuilder: (_, index) {
                    final reason = refundReasonList[index];
                    return GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      onTap: () {
                        if (!(select == reason)) {
                          _indexNotifier.value = reason;
                        }
                      },
                      child: Container(
                        margin: EdgeInsets.only(top: index == 0 ? XfgSize().size18:XfgSize().size24),
                        padding: EdgeInsets.symmetric(horizontal: XfgSize().size10),
                        child: Row(
                          children: [
                            Expanded(child: XfgText(reason.desc, fontSize: XfgSize().text16, color: XfgColor.text1D)),
                            Image.asset(select == reason ? Res.imagesPayRadioS:Res.imagesPayRadio, width: XfgSize().size18, height: XfgSize().size18)
                          ],
                        ),
                      ),
                    );
                  },
                ),
              ),
            ),
            const Spacer(),
            TextButton(
              onPressed: () {
                final reason = _indexNotifier.value;
                if (reason != null) {
                  XfgNav.pop(reason);
                } else {
                  '请选择取消原因'.info();
                }
              },
              style: xfgButtonStyle(Size(XfgSize().size342, XfgSize().size50), radius: XfgSize().size50),
              child: XfgText('下一步', fontSize: XfgSize().text16, color: XfgColor.white),
            )
          ],
        ),
      ),
    );
  }
}